// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { _decorator, Component, log, UIOpacity, Vec3 } from 'cc';
import Game from './Game';
import Player from './Player';
const { ccclass, property } = _decorator;

@ccclass('Star')
export default class Star extends Component {
    @property
    private pickRadius: number = 0;
    @property(Game)
    public game: Game = null!;
    @property(UIOpacity)
    public uiOpacity: UIOpacity = null!;
    // LIFE-CYCLE CALLBACKS:
    // onLoad () {}
    getPlayerDistance() {
        let playerPos = (<Player>this.game.player.getComponent("Player")).getCurPosition();
        let lenVec = new Vec3();
        Vec3.subtract(lenVec, this.node.position, playerPos);
        return lenVec.length();
    }
    onPicked() {
        this.game.spawnNewStar();
        this.game.gainScore();
        this.node.destroy();
    }
    start() {
    }
    update(dt: number) {
        if (this.getPlayerDistance() < this.pickRadius) {
            this.onPicked();
            return;
        }
        let opacityRatio = 1 - this.game.timer / this.game.starDuration;
        let minOpacity = 50;
        this.uiOpacity.opacity = minOpacity + Math.floor(opacityRatio * (255 - minOpacity));
    }
}

